package com.union.vehicleassistant.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;

import com.union.vehicleassistant.R;

/**
 * 这个类可以用于创建简单的 TextView 显示类型的 Header。
 * <p>
 * Created by muyangmin on Dec 29, 2017.
 */
public abstract class SimpleTextHeader implements StickyHeaderAdapter<SimpleHolder> {

    @ColorInt
    private int mThemeBackgroundColor = -1;
    @ColorInt
    private int mThemeTextColor = -1;

    /**
     * 默认会创建一个 TextView, 其水平 padding 为10dp，竖直 padding 为 8dp，
     * 使用 {@code colorPrimary} 来作为背景， {@code textColorPrimary} 为文字颜色。
     */
    @SuppressLint("ResourceType")
    protected TextView createTextView(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_header, null, false);
        /*TextView textView = new TextView(context);
        ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams
                .MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

        int verticalPadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                8F, textView.getResources().getDisplayMetrics());

        int horizontalPadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                15F, textView.getResources().getDisplayMetrics());

        textView.setPadding(horizontalPadding, verticalPadding, horizontalPadding, verticalPadding);

        textView.setLayoutParams(lp);
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
        if (mThemeBackgroundColor == -1 || mThemeTextColor == -1) {
            int[] attrs = {android.R.attr.colorPrimary, android.R.attr.textColorPrimary};
            TypedArray array = context.obtainStyledAttributes(attrs);
            mThemeBackgroundColor = array.getColor(0, Color.WHITE);
            mThemeTextColor = array.getColor(1, Color.BLACK);
            array.recycle();
        }
        textView.setBackgroundColor(mThemeBackgroundColor);
        textView.setTextColor(mThemeTextColor);*/
        return view.findViewById(R.id.tv_header);
    }

    /**
     * 为 Header 指定要展示的文本。
     *
     * @param childPos item 在数据集中的位置
     */
    protected abstract CharSequence getHeaderContent(int childPos);

    @NonNull
    @Override
    public SimpleHolder onCreateHeaderViewHolder(ViewGroup parent) {
        TextView textView = createTextView(parent.getContext());
        return new SimpleHolder(textView);
    }

    @Override
    public void onBindHeaderViewHolder(@NonNull SimpleHolder holder, int childAdapterPosition) {
        ((TextView) holder.itemView).setText(getHeaderContent(childAdapterPosition));
    }
}